package gui;

import java.awt.event.*;

import javax.swing.JInternalFrame;
import controllers.GUIController;
import hibernate.User;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class AMyAccount extends JInternalFrame {

    private javax.swing.JPanel acctInfoPanel;
    private javax.swing.JPanel bkgrd;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton editButton;
    private javax.swing.JLabel studentCountLabel;
    private javax.swing.JButton homeButton;
    private javax.swing.JLabel logo;
    private javax.swing.JPanel logoPanel;
    private javax.swing.JButton logoutButton;
    private javax.swing.JButton myaccountButton;
    private javax.swing.JLabel myaccountLabel;
    private javax.swing.JPanel nameGradeInfoPanel;
    private javax.swing.JLabel nameLabel;
    private JButton activitiesButton;
    final AMyAccount obj = this;
    private GUIController guiC;
    private List<User> sList;

    public AMyAccount(GUIController m) throws IOException {
        guiC = m;
        generateLayout();
        addMyAccountActionListener();
        addHomeActionListener();
        addLogoutActionListener();
        addEditActionListener();
        addActivitiesActionListener();
        updatePersonalLabels();
        showStudentButtons();
    }

    private void addActivitiesActionListener() {
        activitiesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AAddActivity(guiC));
                obj.dispose();
            }
        });
    }

    private void updatePersonalLabels() {
        User u = guiC.currentUser;
        List l = guiC.getStudentList(u);
        nameLabel.setText("Name: " + u.getCommonName());
        studentCountLabel.setText("Class Size: " + l.size());
    }

    public void updateStudentList() {
        List tempList = guiC.getStudentList(guiC.currentUser);
        sList = tempList;
    }

    private void showStudentButtons() throws IOException {
        updateStudentList();
        if (sList.size() >= 1) {
            User u;
            for (Iterator i = sList.iterator(); i.hasNext();) {
                u = (User) i.next();
                if (u == null) {
                    guiC.showGUI(new ALogin(guiC));
                    obj.dispose();
                } else {
//                u.getButton().setText(u.getFirstName() + " " + u.getLastName());
                    JButton b = new JButton(); 
                    u.setStudentButton(b);
                    }
                }
            }
        }

    // adds actionlistener to the logout button to log the admin out of their
    // account
    private void addLogoutActionListener() {
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // show login page/close application on logout
                guiC.showGUI(new ALogin(guiC));
                // close previous gui(AMyClass)
                obj.dispose();
            }
        });
    }

    // adds actionlistener to myaccount button to go to the page My Class
    private void addMyAccountActionListener() {
        myaccountButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    guiC.showGUI(new AMyAccount(guiC));
                } catch (IOException ex) {
                    Logger.getLogger(AMyAccount.class.getName()).log(Level.SEVERE, null, ex);
                }
                obj.dispose();
            }
        });
    }

    // adds actionlistener to edit button to go to the page editUser page
    private void addEditActionListener() {
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AEditUser(guiC));
                obj.dispose();
            }
        });
    }

    //adds actionlistener to home button to go to the home page
    private void addHomeActionListener() {
        homeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AMyClass(guiC));
                obj.dispose();
            }
        });
    }

    public void generateLayout() {
        bkgrd = new javax.swing.JPanel();
        logoPanel = new javax.swing.JPanel();
        logo = new javax.swing.JLabel();
        buttonPanel = new javax.swing.JPanel();
        homeButton = new javax.swing.JButton();
        myaccountButton = new javax.swing.JButton();
        logoutButton = new javax.swing.JButton();
        acctInfoPanel = new javax.swing.JPanel();
        nameGradeInfoPanel = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        studentCountLabel = new javax.swing.JLabel();
        editButton = new javax.swing.JButton();
        myaccountLabel = new javax.swing.JLabel();
        activitiesButton = new JButton("Activities");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OnPoint Scheduler- My Account");

        bkgrd.setBackground(new java.awt.Color(0, 102, 255));
        bkgrd.setToolTipText("");

        logoPanel.setOpaque(false);

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/OnPointlogo2-1.png"))); // NOI18N

        javax.swing.GroupLayout logoPanelLayout = new javax.swing.GroupLayout(logoPanel);
        logoPanel.setLayout(logoPanelLayout);
        logoPanelLayout.setHorizontalGroup(
                logoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(logoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logo)
                .addContainerGap(23, Short.MAX_VALUE)));
        logoPanelLayout.setVerticalGroup(
                logoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(logo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

        buttonPanel.setOpaque(false);

        homeButton.setText("Home");
        homeButton.setOpaque(false);

        myaccountButton.setText("MyAccount");
        myaccountButton.setOpaque(false);

        logoutButton.setText("Logout");
        logoutButton.setOpaque(false);

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
                buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(myaccountButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(homeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(activitiesButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(logoutButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        buttonPanelLayout.setVerticalGroup(
                buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(homeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(myaccountButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(activitiesButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logoutButton)
                .addContainerGap(46, Short.MAX_VALUE)));

        acctInfoPanel.setBackground(new java.awt.Color(51, 153, 255));
        acctInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Personal Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Aharoni", 0, 14))); // NOI18N

        nameGradeInfoPanel.setBackground(new java.awt.Color(51, 153, 255));

        nameLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        nameLabel.setText("Name: ");

        studentCountLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        studentCountLabel.setText("Student Count: ");

        javax.swing.GroupLayout nameGradeInfoPanelLayout = new javax.swing.GroupLayout(nameGradeInfoPanel);
        nameGradeInfoPanel.setLayout(nameGradeInfoPanelLayout);
        nameGradeInfoPanelLayout.setHorizontalGroup(
                nameGradeInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(nameGradeInfoPanelLayout.createSequentialGroup()
                .addGroup(nameGradeInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(nameLabel)
                .addComponent(studentCountLabel))
                .addGap(0, 170, Short.MAX_VALUE)));
        nameGradeInfoPanelLayout.setVerticalGroup(
                nameGradeInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(nameGradeInfoPanelLayout.createSequentialGroup()
                .addComponent(nameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(studentCountLabel)
                .addContainerGap(28, Short.MAX_VALUE)));

        editButton.setText("Edit");
        editButton.setOpaque(false);



        javax.swing.GroupLayout acctInfoPanelLayout = new javax.swing.GroupLayout(acctInfoPanel);
        acctInfoPanel.setLayout(acctInfoPanelLayout);
        acctInfoPanelLayout.setHorizontalGroup(
                acctInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(acctInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(acctInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(acctInfoPanelLayout.createSequentialGroup()
                .addComponent(nameGradeInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 243, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, acctInfoPanelLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(editButton, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap()));
        acctInfoPanelLayout.setVerticalGroup(
                acctInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(acctInfoPanelLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(nameGradeInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGap(18, 18, 18)
                .addComponent(editButton)
                .addContainerGap()));

        myaccountLabel.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        myaccountLabel.setText("My Account");

        javax.swing.GroupLayout bkgrdLayout = new javax.swing.GroupLayout(bkgrd);
        bkgrd.setLayout(bkgrdLayout);
        bkgrdLayout.setHorizontalGroup(
                bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(myaccountLabel)
                .addComponent(acctInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addComponent(logoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(715, Short.MAX_VALUE)));
        bkgrdLayout.setVerticalGroup(
                bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(311, 311, 311))
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(myaccountLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(acctInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE)))));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(bkgrd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(bkgrd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

        pack();
    }
}
